package com.automation.entity;
import com.automation.service.AppiumUtils;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.By;

public class SearchPage extends BasePage {
    private final By searchInput = By.id("com.dianping.v1:id/search_input");
    private final By searchButton = By.id("com.dianping.v1:id/search_button");
    private final By resultContainer = By.id("com.dianping.v1:id/search_result_container");

    public SearchPage(AndroidDriver driver) {
        super(driver);
    }

    public void searchFor(String keyword) {
        sendKeys(searchInput, keyword);
        click(searchButton);
        AppiumUtils.waitForElement(driver, resultContainer, 15);
    }

    public boolean isKeywordEntered(String keyword) {
        return getText(searchInput).contains(keyword);
    }

    public void selectShopWithAd(String shopName) {
        By shopLocator = By.xpath(String.format(
                "//*[contains(@text, '%s') and contains(@text, '广告')]",
                shopName
        ));
        AppiumUtils.scrollToText(driver, shopName);
        click(shopLocator);
    }
}